package com.vesystem.vegua.modules.common.controller;


import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vesystem.vegua.config.GlobalConfig;
import com.vesystem.vegua.exceptionConfig.ErrorCode;
import com.vesystem.vegua.exceptionConfig.ParameterInvalid;
import com.vesystem.vegua.modules.common.dto.MappingUserAppDto;
import com.vesystem.vegua.modules.common.dto.MonitoringFileDto;
import com.vesystem.vegua.modules.common.dto.RemoteAppDto;
import com.vesystem.vegua.modules.common.dto.SysIconDto;
import com.vesystem.vegua.modules.common.entity.RemoteApp;
import com.vesystem.vegua.modules.common.mapper.SysIconMapper;
import com.vesystem.vegua.modules.common.service.impl.MappingUserAppServiceImpl;
import com.vesystem.vegua.modules.common.service.impl.RemoteAppServiceImpl;
import org.apache.ibatis.annotations.Delete;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hcy
 * @since 2020-12-08
 */
@RestController
@RequestMapping("remoteApp")
public class RemoteAppController {

    @Autowired
    private RemoteAppServiceImpl remoteAppService;

    @Autowired
    private MappingUserAppServiceImpl mappingUserAppService;



    @PostMapping("addApp")
    public void addApp(@RequestBody RemoteAppDto remoteApp){
        remoteAppService.addApp(remoteApp);
    }

    @GetMapping("getRemoteAppInfoById/{appId}")
    public RemoteApp getRemoteAppInfoById(@PathVariable Integer appId){
        return remoteAppService.getRemoteAppInfoById(appId);
    }

    @PutMapping("updateRemoteApp")
    public void updateRemoteApp(@RequestBody RemoteAppDto remoteApp){
        remoteAppService.updateRemoteApp(remoteApp);
    }

    @DeleteMapping("deleteRemoteAppById/{appId}")
    public void deleteRemoteAppById(@PathVariable Integer appId){
        remoteAppService.deleteRemoteAppById(appId);
    }

    @GetMapping("adminGetAllRemoteAppList")
    public Page<RemoteApp> adminGetAllRemoteAppList(Page<RemoteApp> page, String appName,String serverIp){
        return remoteAppService.adminGetAllRemoteAppList(page,appName,serverIp);
    }

    @GetMapping("userGetRemoteAppList")
    public List<RemoteApp> userGetRemoteAppList(HttpServletRequest request){
        return remoteAppService.userGetRemoteAppList(request);
    }

    @PutMapping("updateMappingUserApp")
    public void updateMappingUserApp(@RequestBody MappingUserAppDto userAppDto){
        mappingUserAppService.updateMappingUserApp(userAppDto.getUserId(),userAppDto.getAppIds());
    }

    @GetMapping("getAppIdsByUserId/{userId}")
    public List<Integer> getAppIdsByUserId(@PathVariable Integer userId){
        return mappingUserAppService.getAppIdsByUserId(userId);
    }

    @GetMapping("goToRemotePage")
    public void goToRemotePage(HttpServletRequest request,HttpServletResponse response,Integer connectionParam,String token) throws IOException {
        remoteAppService.goToRemotePage(request,response,connectionParam,token);
    }

    @PostMapping("uploadIcon")
    public void uploadIcon(MultipartFile file){
        remoteAppService.uploadIcon(file);
    }

    @DeleteMapping("deleteIcon")
    public void deleteIcon(@RequestBody Map<String,String> map){
        remoteAppService.deleteIcon(map.get("iconName"));
    }

    @GetMapping("getIconList")
    public Page<SysIconDto> getIconList(Page<SysIconDto> page){
        return remoteAppService.getIconList(page);
    }

    @PutMapping("updateAppIcon")
    public void updateAppIcon(@RequestBody Map<String,Object> param){
        remoteAppService.updateAppIcon((List<Integer>) param.get("appIds"),(String) param.get("iconUrl"));
    }

    @GetMapping("selectAppIdsByIconName")
    public List<Integer> selectAppIdsByIconName(String iconName){
        return remoteAppService.selectAppIdsByIconName(iconName);
    }

    @GetMapping("getAllRemoteMonitoringAppIds")
    public List<Integer> getAllRemoteMonitoringAppIds(){
        return remoteAppService.getAllRemoteMonitoringAppIds();
    }

    @PutMapping("updateRemoteMonitoringList")
    public void updateRemoteMonitoringList(@RequestBody Map<String,List<Integer>> param){
        remoteAppService.updateRemoteMonitoringList(param.get("ids"));
    }

    @GetMapping("getMonitoringFileDtoList")
    public List<MonitoringFileDto> getMonitoringFileDtoList(String remoteAlias,Long startTime,Long endTime){
        return remoteAppService.getMonitoringFileDtoList(remoteAlias,startTime,endTime);
    }








}

